﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebDauGia.Domain.Abstract;
//using WebDauGia.Domain.Entities;
using WebDauGia.Domain.Models;
using WebDauGia.Models;
using WebDauGia.HtmlHelpers;
using WebDauGia.Domain.Concrete;

namespace WebDauGia.Controllers
{
    public class SanPhamController : Controller
    {
        //
        // GET: /SanPham/
        //EFDbContext db = new EFDbContext();
        WebDauGiaDBContext db = new WebDauGiaDBContext();
        private WebRepository repository;
        public int PageSize = 6;
        public SanPhamController(WebRepository productRepository) {
            this.repository = productRepository;
        }
        public ViewResult List(string loaisp, int page=1) {
            /*var sp = (from p in db.SanPhams
                      join l in db.LoaiSanPhams
                      on p.idloaisp equals l.id
                      where p.idloaisp == l.id
                      select p).AsQueryable();*/
            SanPhamsListViewModel model = new SanPhamsListViewModel
            {
                #region[code]
                SanPhams = repository.SanPhams
                .Where(p => loaisp == null|| p.idloaisp.ToString()==loaisp && p.active==true)
                .OrderBy(p => p.id)
                .Skip((page - 1) * PageSize)
                .Take(PageSize),
                PagingInfo = new PagingInfo { 
                    CurrentPage=page,
                    ItemsPerPage = PageSize,
                    TotalItems = loaisp ==null?
                    repository.SanPhams.Count() :
                    repository.SanPhams.Where(e => e.idloaisp.ToString() == loaisp).Count()
        
                },
                CurrentCategory = loaisp
                #endregion
                /*SanPhams =sp
                .OrderBy(p => p.id)
                .Skip((page - 1) * PageSize)
                .Take(PageSize),
                PagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = PageSize,
                    TotalItems = loaisp == null ?
                    sp.Count() :
                    sp.Where(e => e.idloaisp.ToString() == loaisp).Count()

                },
                CurrentCategory = loaisp*/
            };
            return View(model);
        }
        public ActionResult DSLoaiSanPham()
        {
            var loaisp = repository.LoaiSanPhams.ToList();/*db.LoaiSanPhams.ToList();*/
            return PartialView(loaisp);
        }
        public ActionResult ChiTietSanPham(int id)
        {
            SanPham s = db.SanPhams.Single(p => p.id == id);
            return View(s);
        }
        public ActionResult DSSanPhamTheoLoai(int id=0, int page = 1)
        {
           /* ViewBag.id = id;
            var sp = (from p in db.SanPhams
                      where p.idloaisp == id
                      select p).ToList();
            ViewBag.TotalPages = Math.Ceiling((double)sp.Count / PageSize);
            return View(sp.Skip((page - 1) * PageSize).Take(PageSize));
            */
            SanPhamsListViewModel model = new SanPhamsListViewModel
            {
                
                SanPhams = repository.SanPhams
                .Where(p => id == null|| p.idloaisp==id)
                .OrderBy(p => p.id)
                .Skip((page - 1) * PageSize)
                .Take(PageSize),
                PagingInfo = new PagingInfo { 
                    CurrentPage=page,
                    ItemsPerPage = PageSize,
                    TotalItems = id ==null?
                    repository.SanPhams.Count() :
                    repository.SanPhams.Where(e => e.idloaisp == id).Count()
        
                }
                //CurrentCategory = id.ToString()
            };
            return View(model);
        }
        
    }
}
